<?php

namespace App\Http\Controllers;

use App\Models\AppUserSmsCode;
use App\Models\UserSmsCode;
use Illuminate\Http\Request;

class AppController extends Controller
{
    protected $failed_code = [
        1   => '参数错误',
        2   => '角色限制',
        3   => '支付系统异常',
        4   => '角色异常',
    ];

    protected $error_code = [
        1   => '帐号或密码错误',
        2   => '验证码不正确',
        3   => '验证码已过期',
        4   => '发送失败',
        5   => '发送频繁，请稍后再试',
        6   => '数据有误',
        7   => '微信登录失败',
        8   => '支付类型不存在',
        9   => '该身份已在申请中',
        10  => '该商品已下架',
        11  => '生成订单失败',
        12  => '余额不足',
        13  => '订单不存在或已关闭，请刷新订单列表',
        14  => '订单状态已改变请刷新后重试',
        15  => '退款失败',
        16  => '已有一笔退款金额真正审核',
        17  => '地址不存在，请重试',
        18  => '物流查询失败',
        19  => '帐号已存在',
        20  => '您还不是经销商',

        101 => '微信尚未注册',
        102 => '未获取openId，请重新登录',
        103 => 'code无效',
        104 => '未绑定手机号',
        105 => '无权操作',

        201 => '该帐号还没有物流身份',
        202 => '接单失败!',
        203 => '还有订单未完成，请勿下线',
    ];

    /**
     * 获取身份
     * @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|\Illuminate\Contracts\Foundation\Application
     */
    protected function auth()
    {
        return auth('app');
    }

    /**
     * 验证手机验证码
     *
     * @param int $mobile
     * @param int $code
     * @return false|int
     */
    protected function verifySmsCode(int $mobile, int $code)
    {
        $elem = UserSmsCode::where([
            ['mobile',$mobile],
            ['code',$code],
            ['status',0],
        ])->latest()->first();

        if (! $elem){

            return 2;
        }elseif ((time() - strtotime($elem->created_at)) > 300){

            return 3;
        }

        $elem->increment('status');

        return false;
    }

    /**
     * 返回失败
     * @param int $code
     * @param array $data
     * @return \Illuminate\Http\JsonResponse
     */
    protected function ReturnFailed(int $code, $data = [])
    {
        $data = collect($data)->toArray();

        return response()->json([
            'code'  => $code,
            'msg'   => $this->failed_code[$code],
            'data'  => $data
        ]);
    }

    /**
     * 返回成功
     * @param array $data
     * @return \Illuminate\Http\JsonResponse
     */
    protected function ReturnSuccessInfo($data = [])
    {
        $data = collect($data)->toArray();

        return response()->json([
            'code'  => 0,
            'msg'   => 'ok',
            'data'  => array_merge([
                'error'     => 0,
                'message'   => 'ok',
            ],$data),
        ]);
    }

    /**
     * 返回验证失败消息
     * @param int $code
     * @param array $data
     * @return \Illuminate\Http\JsonResponse
     */
    protected function ReturnVerifyError(int $code, $data = [])
    {
        $data = collect($data)->toArray();

        $res = array_merge([
            'error'     => $code,
            'message'   => $this->error_code[$code],
        ],$data);

        return $this->ReturnSuccessInfo($res);
    }

    /**
     * 返回自定义信息
     * @param int $code
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     */
    protected function ReturnMsg(int $code,$msg = '')
    {
        return $this->ReturnSuccessInfo([
            'error'  => $code,
            'message'=> $msg,
        ]);
    }

    /**
     * 返回分页数据
     * @param array $list
     * @param array $other
     * @return mixed|\Symfony\Component\HttpFoundation\ParameterBag|null
     */
    protected function ReturnPage($list = [], $other = [])
    {

        $data = [
            'code' => 0,
            'msg'   =>'',
            'data'  => $list->items(),
            'page'  => $list->currentPage(),
            'total_page' => $list->lastPage(),
            'num'   => $list->count(),
            'count' => $list->total(),
            'other' => $other
        ];

        return response()->json($data);
    }
}
